﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.ComponentModel;

namespace CVTH.Helper
{
    public class UserHelper
    {
        public const string AnonymousUsername = "Anonymous";
        public static Guid CurrentUserId4
        {
            get
            {
                return (Guid)Membership.GetUser().ProviderUserKey;
            }
        }
        public static string GetUserKey(string username)
        {
            try
            {
                return Membership.GetUser(username).ProviderUserKey.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }
        public static string GetUserName(string userkey)
        {
            try
            {
                return Membership.GetUser(new Guid(userkey)).UserName;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string UserName
        {
            get
            {

                string Name = (HttpContext.Current.User == null) ? "" : HttpContext.Current.User.Identity.Name;
                if (string.IsNullOrEmpty(Name)) Name = AnonymousUsername;
                return Name;
            }
        }

        public static bool IsAuthenticated
        {
            get
            {
                return HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }

        public static bool IsAdmin
        {
            get
            {
                return HttpContext.Current.User.IsInRole("admin");
            }
        }
       
        public static bool HasRole(string RoleName)
        {
            return HttpContext.Current.User.IsInRole(RoleName);
        }
        public static ProfileBase CurrentProfile
        {
            get
            {
                return HttpContext.Current.Profile;
            }
        }
        public static string Language
        {
            get
            {
                object sess = (HttpContext.Current.Session != null) ? HttpContext.Current.Session["Language"] : null;
                return (sess == null) ? "vi-VN" : sess.ToString();
            }
            set
            {
                HttpContext.Current.Session["Language"] = value;
            }
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public MembershipUserCollection FindAllUser(int maximumRows, int startRowIndex)
        {
            //int totalCount= 0;
            //var a = Membership.GetAllUsers(startRowIndex, maximumRows, out totalCount);

            return Membership.GetAllUsers();
        }



        #region Data source

        static int count;

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static int SelectCount(int maximumRows, int startRowIndex, string filter, string filterType)
        {
            return count;
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public static MembershipUserCollection Select(int maximumRows, int startRowIndex, string filter, string filterType)
        {
            MembershipUserCollection collection;
            if (string.IsNullOrEmpty(filter)) collection = Membership.GetAllUsers(startRowIndex / maximumRows, maximumRows, out count);
            else if (filterType == "ByName") collection = Membership.FindUsersByName(filter, startRowIndex / maximumRows, maximumRows, out count);
            else collection = Membership.FindUsersByEmail(filter, startRowIndex / maximumRows, maximumRows, out count);
            if (HttpContext.Current != null) HttpContext.Current.Items["rowCount"] = count;
            return collection;
        }

        [DataObjectMethod(DataObjectMethodType.Delete)]
        public static void Delete(string UserName)
        {

            Membership.DeleteUser(UserName, true);

        }

        #endregion


    }
}